package com.pet.server.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pet.server.domain.entity.Pet;
import com.pet.server.service.IPetService;
import com.pet.server.utils.ResultUtil;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@WebServlet(name = "PetServlet", urlPatterns = "/PetServlet")
public class PetServlet extends HttpServlet {
  @Resource IPetService sp;

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String method = request.getParameter("method");
    if (method.equalsIgnoreCase("page")) {
      PetPage(request, response);
    } else if (method.equalsIgnoreCase("add")) {
      AddPet(request, response);
    } else if (method.equalsIgnoreCase("del")) {
      DelPet(request, response);
    } else if (method.equalsIgnoreCase("update")) {
      UpdatePet(request, response);
    } else if (method.equalsIgnoreCase("select")) {
      SelectPet(request, response);
    }
  }

  /**
   * 分页多条件模糊查询
   *
   * @param request
   * @param response
   */
  private void SelectPet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    int pageNo = Integer.parseInt(request.getParameter("pageNo"));
    int pageSize = Integer.parseInt(request.getParameter("pageSize"));
    String name = request.getParameter("name");
    String category = request.getParameter("category");
    String type = request.getParameter("type");
    String colour = request.getParameter("colour");
    String character = request.getParameter("character");
    int petMasterId = Integer.parseInt(request.getParameter("petMasterId"));
    Pet pet = new Pet();
    //    pet.setMasterId(petMasterId);
    pet.setPetName(name);
    pet.setPetCategory(category);
    pet.setPetType(type);
    pet.setPetCharacter(character);
    pet.setPetColour(colour);
    List<Pet> list = sp.getPetBySelect(pageNo, pageSize, pet);
    ResultUtil<List> resultUtil = null;
    if (list != null) {
      resultUtil = new ResultUtil<>(list);
    }
    ObjectMapper objectMapper = new ObjectMapper();
    String json = objectMapper.writeValueAsString(objectMapper);
    response.getWriter().write(json);
  }

  /**
   * 更新宠物信息
   *
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException
   */
  private void UpdatePet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String name = request.getParameter("name");
    String category = request.getParameter("category");
    String type = request.getParameter("type");
    String birthday = request.getParameter("birthday");
    String colour = request.getParameter("colour");
    String character = request.getParameter("character");
    Pet pet = new Pet();
    pet.setPetName(name);
    pet.setPetCategory(category);
    pet.setPetType(type);
    DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    LocalDate birthday2 = LocalDate.parse(birthday, fmt);
    pet.setPetBirthday(birthday2);
    pet.setPetCharacter(character);
    pet.setPetColour(colour);
    boolean b = sp.Update(pet);
    response.getWriter().write(String.valueOf(b));
  }

  /**
   * 删除宠物
   *
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException
   */
  private void DelPet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    int id = Integer.parseInt(request.getParameter("id"));
    boolean b = sp.DelPet(id);
    response.getWriter().write(String.valueOf(b));
  }

  /**
   * 添加宠物
   *
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException
   */
  private void AddPet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    int petMasterId = Integer.parseInt(request.getParameter("petMasterId"));
    String name = request.getParameter("name");
    String category = request.getParameter("category");
    String type = request.getParameter("type");
    String birthday = request.getParameter("birthday");
    String colour = request.getParameter("colour");
    String character = request.getParameter("character");

    DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    LocalDate birthday2 = LocalDate.parse(birthday, fmt);
    Pet pet = new Pet();
    //    pet.setMasterId(petMasterId);
    pet.setPetName(name);
    pet.setPetCategory(category);
    pet.setPetType(type);
    pet.setPetBirthday(birthday2);
    pet.setPetCharacter(character);
    pet.setPetColour(colour);
    boolean b = sp.AddPet(pet);
    response.getWriter().write(String.valueOf(b));
  }

  /**
   * 分页查询
   *
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException
   */
  private void PetPage(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    int petMasterId = Integer.parseInt(request.getParameter("petMasterId"));
    int pageNo = Integer.parseInt(request.getParameter("pageNo"));
    int pageSize = Integer.parseInt(request.getParameter("pageSize"));

    List<Pet> list = sp.getAllByPage(pageNo, pageSize, petMasterId);
    ResultUtil<List> resultUtil = null;
    if (list != null) {
      resultUtil = new ResultUtil<>(list);
    }
    ObjectMapper objectMapper = new ObjectMapper();
    String json = objectMapper.writeValueAsString(objectMapper);
    response.getWriter().write(json);
  }
}
